﻿//***************************************************************
//说明：自动生成代码第二版
//时间：2014-08-11 10:39
//***************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using GS.DataBase;

namespace LandWeb.Models
{
    /// <summary>
    /// 表[LARTYPE]存取
    /// 关键字段为[TID]
    /// </summary>
    public class LARTYPE : GS.IGSAccess<LARTYPE>
    {
        #region 属性
        /// <summary>
        /// 字段名称:TID [字段类型:int]
        /// </summary>
        public string TID { get; set; }

        /// <summary>
        /// 字段名称:TNAME [字段类型:nvarchar]
        /// </summary>
        public string TNAME { get; set; }

        /// <summary>
        /// 字段名称:TCODE [字段类型:nvarchar]
        /// </summary>
        public string TCODE { get; set; }

        /// <summary>
        /// 字段名称:INTR [字段类型:nvarchar]
        /// </summary>
        public string INTR { get; set; }

        /// <summary>
        /// 字段名称:TOID [字段类型:int]
        /// </summary>
        public string TOID { get; set; }

        /// <summary>
        /// 字段名称:PTID [字段类型:int]
        /// </summary>
        public string PTID { get; set; }
        #endregion

        /// <summary>
        /// 数据库字段名称
        /// </summary>
        public class Column
        {
            /// <summary>
            /// 字段名称:TID [字段类型:int]
            /// </summary>
            public const string TID = "TID";

            /// <summary>
            /// 字段名称:TNAME [字段类型:nvarchar]
            /// </summary>
            public const string TNAME = "TNAME";

            /// <summary>
            /// 字段名称:TCODE [字段类型:nvarchar]
            /// </summary>
            public const string TCODE = "TCODE";

            /// <summary>
            /// 字段名称:INTR [字段类型:nvarchar]
            /// </summary>
            public const string INTR = "INTR";

            /// <summary>
            /// 字段名称:TOID [字段类型:int]
            /// </summary>
            public const string TOID = "TOID";

            /// <summary>
            /// 字段名称:PTID [字段类型:int]
            /// </summary>
            public const string PTID = "PTID";
        }

        #region 方法
        /// <summary>
        /// 获取一行记录
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sFilter">条件字符串，以AND开头，不能为空</param>
        /// <returns></returns>
        public LARTYPE Get(IDbAccess iDb, string sFilter)
        {
            if (string.IsNullOrWhiteSpace(sFilter))
            {
                throw new Exception("条件字符串不能为空");
            }
            LARTYPE result = new LARTYPE();
            DataSet ds = iDb.GetDataSet("SELECT TID,TNAME,TCODE,INTR,TOID,PTID FROM LARTYPE WHERE 1=1 " + sFilter);
            if (ds.Tables[0].Rows.Count == 1)
            {
                result.TID = ds.Tables[0].Rows[0][Column.TID].ToString();
                result.TNAME = ds.Tables[0].Rows[0][Column.TNAME].ToString();
                result.TCODE = ds.Tables[0].Rows[0][Column.TCODE].ToString();
                result.INTR = ds.Tables[0].Rows[0][Column.INTR].ToString();
                result.TOID = ds.Tables[0].Rows[0][Column.TOID].ToString();
                result.PTID = ds.Tables[0].Rows[0][Column.PTID].ToString();

                return result;
            }
            return null;
        }

        /// <summary>
        /// 插入新记录
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="result">表对象</param>
        /// <returns>保存是否成功</returns>
        public bool Add(IDbAccess iDb, LARTYPE result)
        {
            if (string.IsNullOrWhiteSpace(result.TID.ToString()))
            {
                throw new Exception("关键字的值不能为空");
            }

            Hashtable ht = new Hashtable();
            ht.Add(Column.TID, result.TID);
            ht.Add(Column.TNAME, result.TNAME);
            ht.Add(Column.TCODE, result.TCODE);
            ht.Add(Column.INTR, result.INTR);
            ht.Add(Column.TOID, result.TOID);
            ht.Add(Column.PTID, result.PTID);

            return iDb.AddData("LARTYPE", ht);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="result">表对象</param>
        /// <returns>保存是否成功</returns>
        public bool Save(IDbAccess iDb, LARTYPE result)
        {
            if (string.IsNullOrWhiteSpace(result.TID.ToString()))
            {
                throw new Exception("关键字的值不能为空");
            }

            Hashtable ht = new Hashtable();
            ht.Add(Column.TID, result.TID);
            ht.Add(Column.TNAME, result.TNAME);
            ht.Add(Column.TCODE, result.TCODE);
            ht.Add(Column.INTR, result.INTR);
            ht.Add(Column.TOID, result.TOID);
            ht.Add(Column.PTID, result.PTID);

            return iDb.SaveData("LARTYPE", ht, "TID");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sFilter">条件字符串，以AND开头，不能为空</param>
        /// <returns>返回删除的记录行数</returns>
        public int Delete(IDbAccess iDb, string sFilter)
        {
            if (string.IsNullOrWhiteSpace(sFilter))
            {
                throw new Exception("条件字符串不能为空");
            }
            return iDb.DeleteTableRow("LARTYPE", " 1=1 " + sFilter);
        }

        /// <summary>
        /// 删除分类，子节点一并删除
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="iTypeID">分类ID</param>
        public void DelNode(IDbAccess iDb, int iTypeID)
        {
            //先判断是否有子节点
            string strSql = string.Format("select TID from LARTYPE where PTID={0}", iTypeID);
            DataSet ds = iDb.GetDataSet(strSql);
            int iCount = ds.Tables[0].Rows.Count;
            for (int i = 0; i < iCount; i++)
            {
                if (ds.Tables[0].Rows[i]["TID"] == null || ds.Tables[0].Rows[i]["TID"].ToString().Length == 0)
                {
                    continue;
                }
                int iID = Convert.ToInt32(ds.Tables[0].Rows[i]["TID"].ToString());
                DelNode(iDb, iID);
            }

            strSql = string.Format("delete from LARTYPE where TID={0}", iTypeID);
            iDb.ExecuteSql(strSql);

        }

        /// <summary>
        /// 获得指定过滤条件的记录行数
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sFilter">查询条件，以AND开头,可以为空</param>
        /// <returns>数据库中的记录行数</returns>
        public int GetDataCount(IDbAccess iDb, string sFilter)
        {
            string strSql = "select count(*) from LARTYPE where 1=1 " + sFilter;
            return Convert.ToInt32(iDb.GetFirstColumn(strSql));
        }

        /// <summary>
        /// 返回全部数据List
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sFilter">查询条件，以AND开头,可以为空</param>
        /// <param name="sOrderColumn">排序字段，可以指定多个字段，以','分隔</param>
        /// <param name="sOrderType">排序类型 ASC/DESC</param>
        /// <returns>返回全部数据List</returns>
        public List<LARTYPE> SearchList(IDbAccess iDb, string sFilter, string sOrderColumn, string sOrderType)
        {
            return SearchListByPage(iDb, "TID", 0, 0, sFilter, sOrderColumn, sOrderType).ListResult;
        }

        /// <summary>
        /// 返回分页数据List
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sKey">表关键字</param>
        /// <param name="iPageSize">单页记录行数</param>
        /// <param name="iPageIndex">分页索引</param>
        /// <param name="sFilter">查询条件，以AND开头,可以为空</param>
        /// <param name="sOrderColumn">排序字段，可以指定多个字段，以','分隔</param>
        /// <param name="sOrderType">排序类型 ASC/DESC</param>
        /// <returns>返回分页数据List</returns>
        public SearchListResult<LARTYPE> SearchListByPage(IDbAccess iDb, string sKey, int iPageSize, int iPageIndex, string sFilter, string sOrderColumn, string sOrderType)
        {
            SearchListResult<LARTYPE> slr = new SearchListResult<LARTYPE>();
            slr.Count = GetDataCount(iDb, sFilter);
            string sOrderString = "";
            if (sOrderColumn != "")
            {
                sOrderString = " ORDER BY " + sOrderColumn;
                if (sOrderType.ToLower() == "desc")
                {
                    sOrderString += " DESC ";
                }
                else
                {
                    sOrderString += " ASC ";
                }
            }
            sFilter = " 1=1 " + sFilter;
            string strSql = "SELECT TID,TNAME,TCODE,INTR,TOID,PTID FROM LARTYPE t WHERE " + sFilter + sOrderString;
            if (iPageSize > 0)
            {
                strSql = iDb.GetSqlForPageSize("LARTYPE", "TID", iPageSize, iPageIndex, sFilter, sOrderString);
            }
            DataSet ds = iDb.GetDataSet(strSql);
            List<LARTYPE> list = new List<LARTYPE>();
            for (int ii = 0; ii < ds.Tables[0].Rows.Count; ii++)
            {
                LARTYPE result = new LARTYPE();
                result.TID = ds.Tables[0].Rows[ii][Column.TID].ToString();
                result.TNAME = ds.Tables[0].Rows[ii][Column.TNAME].ToString();
                result.TCODE = ds.Tables[0].Rows[ii][Column.TCODE].ToString();
                result.INTR = ds.Tables[0].Rows[ii][Column.INTR].ToString();
                result.TOID = ds.Tables[0].Rows[ii][Column.TOID].ToString();
                result.PTID = ds.Tables[0].Rows[ii][Column.PTID].ToString();

                list.Add(result);
            }
            slr.ListResult = list;
            return slr;
        }

        /// <summary>
        /// 返回全部数据DataSet
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sFilter">查询条件，以AND开头,可以为空</param>
        /// <param name="sOrderColumn">排序字段，可以指定多个字段，以','分隔</param>
        /// <param name="sOrderType">排序类型 ASC/DESC</param>
        /// <returns>返回全部数据DataSet</returns>
        public DataSet Search(IDbAccess iDb, string sFilter, string sOrderColumn, string sOrderType)
        {
            string sOrderString = "";
            if (sOrderColumn != "")
            {
                sOrderString = " ORDER BY " + sOrderColumn;
                if (sOrderType.ToLower() == "desc")
                {
                    sOrderString += " DESC ";
                }
                else
                {
                    sOrderString += " ASC ";
                }
            }
            sFilter = " 1=1 " + sFilter;
            string strSql = "SELECT TID,TNAME,TCODE,INTR,TOID,PTID FROM LARTYPE t WHERE " + sFilter + sOrderString;
            DataSet ds = iDb.GetDataSet(strSql);
            return ds;
        }

        /// <summary>
        /// 返回分页数据DataSet
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sKey">表关键字</param>
        /// <param name="iPageSize">单页记录行数</param>
        /// <param name="iPageIndex">分页索引</param>
        /// <param name="sFilter">查询条件，以AND开头,可以为空</param>
        /// <param name="sOrderColumn">排序字段，可以指定多个字段，以','分隔</param>
        /// <param name="sOrderType">排序类型 ASC/DESC</param>
        /// <returns>返回分页数据DataSet</returns>
        public SearchResult SearchByPage(IDbAccess iDb, string sKey, int iPageSize, int iPageIndex, string sFilter, string sOrderColumn, string sOrderType)
        {
            SearchResult sr = new SearchResult();
            sr.Count = GetDataCount(iDb, sFilter);
            string sOrderString = "";
            if (sOrderColumn != "")
            {
                sOrderString = " ORDER BY " + sOrderColumn;
                if (sOrderType.ToLower() == "desc")
                {
                    sOrderString += " DESC ";
                }
                else
                {
                    sOrderString += " ASC ";
                }
            }
            sFilter = " 1=1 " + sFilter;
            string strSql = "SELECT TID,TNAME,TCODE,INTR,TOID,PTID FROM LARTYPE t WHERE " + sFilter + sOrderString;
            if (iPageSize > 0)
            {
                strSql = iDb.GetSqlForPageSize("LARTYPE", "TID", iPageSize, iPageIndex, sFilter, sOrderString);
            }
            DataSet ds = iDb.GetDataSet(strSql);
            sr.TableResult = ds;
            return sr;
        }
        #endregion

        #region 自定义方法
        #region//获取分类树模型
        /// <summary>
        /// 获取数据字典树模型
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="iShowCheckBox">0/不显示checkbox；1/显示checkbox；</param>
        /// <param name="sFilter">查询条件</param>
        /// <returns>分类树模型</returns>
        public TreeModels GetDictTree(IDbAccess iDb, int iShowCheckBox, int iParentID = 0)
        {
            TreeModels tmModel = new TreeModels();
            string sFilter = string.Format("and PTID=" + iParentID + " order by TOID");
            TreeNode rootNode = new TreeNode();
            #region 处理根节点
            if (iParentID == 0)
            {
                rootNode.Value = "0";
                rootNode.Text = "法规类型";
                rootNode.Obj = "0,-1";//ID,PID
                rootNode.ImageUrl = "../Content/themes/landweb/images/treenode1.png";
                if (iShowCheckBox == 1)
                {
                    rootNode.ShowCheckBox = true;
                }
            }
            else
            {
                string strSqlParent = string.Format("select TID,TNAME,TCODE,INTR,TOID,PTID from LARTYPE where TID='{0}'", iParentID.ToString());
                DataSet dsParent = iDb.GetDataSet(strSqlParent);
                int iCountParent = dsParent.Tables[0].Rows.Count;
                if (iCountParent == 0)
                {
                    return tmModel;
                }
                else
                {
                    rootNode.Text = dsParent.Tables[0].Rows[0]["TNAME"].ToString();
                    rootNode.Value = dsParent.Tables[0].Rows[0]["TCODE"].ToString();
                    rootNode.Obj = dsParent.Tables[0].Rows[0]["TID"].ToString() + "," + dsParent.Tables[0].Rows[0]["PTID"].ToString();
                    rootNode.ImageUrl = "../Content/themes/landweb/images/treenode1.png";
                    if (iShowCheckBox == 1)
                    {
                        rootNode.ShowCheckBox = true;
                    }
                }
            }
            tmModel.ChildNodes.Add(rootNode);
            #endregion
            //根节点
            TreeNode tn = new TreeNode();
            string strSql = "select TID,TNAME,TCODE,INTR,TOID,PTID from LARTYPE where 1=1 " + sFilter;
            DataSet ds = iDb.GetDataSet(strSql);
            int iCount = ds.Tables[0].Rows.Count;
            if (iCount == 0)
            {
                return tmModel;
            }
            for (int i = 0; i < iCount; i++)
            {
                tn = new TreeNode();
                tn.Text = ds.Tables[0].Rows[i]["TNAME"].ToString();
                tn.Value = ds.Tables[0].Rows[i]["TCODE"].ToString();
                tn.Obj = ds.Tables[0].Rows[i]["TID"].ToString() + "," + ds.Tables[0].Rows[i]["PTID"].ToString();
                tn.ImageUrl = "../Content/themes/landweb/images/treenode1.png";
                if (iShowCheckBox == 1)
                {
                    tn.ShowCheckBox = true;
                }
                tn.Expanded = false;
                rootNode.ChildNodes.Add(tn);
                BuildNode(iDb, tn, iShowCheckBox);
            }
            return tmModel;
        }

        /// <summary>
        /// 递归调用问题分类
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="tnRoot">分类节点</param>
        /// <param name="iShowCheckBox">0/不显示checkbox；1/显示checkbox；</param>
        public void BuildNode(IDbAccess iDb, TreeNode tnRoot, int iShowCheckBox)
        {
            string sID = tnRoot.Obj.ToString().Split(',')[0];//分类ID
            if (sID.Length == 0)
            {
                return;
            }

            string strSql = string.Format("select TID,TNAME,TCODE,INTR,TOID,PTID from LARTYPE where PTID={0} order by TOID", sID);
            DataSet ds = iDb.GetDataSet(strSql);
            int iCount = ds.Tables[0].Rows.Count;
            if (iCount == 0)
            {
                return;
            }

            for (int i = 0; i < iCount; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = ds.Tables[0].Rows[i]["TNAME"].ToString();
                tn.Value = ds.Tables[0].Rows[i]["TCODE"].ToString();
                tn.Obj = ds.Tables[0].Rows[i]["TID"].ToString() + "," + ds.Tables[0].Rows[i]["PTID"].ToString();
                tn.ImageUrl = "../Content/themes/landweb/images/treenode2.png";
                if (iShowCheckBox == 1)
                {
                    tn.ShowCheckBox = true;
                }
                tn.Expanded = false;
                tnRoot.ChildNodes.Add(tn);

                BuildNode(iDb, tn, iShowCheckBox);
            }
        }

        #endregion

        public List<LARTYPE> GetChildCode(GS.DataBase.IDbAccess iDb, string sTID, ref List<LARTYPE> listRes)
        {
            LARTYPE larType = new LARTYPE();
            List<LARTYPE> list = larType.SearchList(iDb, " and PTID='" + sTID + "'", "TOID", "ASC");
            foreach (LARTYPE model in list)
            {
                GetChildCode(iDb, model.TID, ref listRes);
                listRes.Add(model);
            }
            return listRes;
        }

        #region//获取父节点名称
        /// <summary>
        /// 获取父节点名称
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sNodeID">节点ID</param>
        /// <returns>父节点名称</returns>
        public string GetNodePath(GS.DataBase.IDbAccess iDb, string sNodeID)
        {
            string sRev = "";
            GetParentName(iDb, sNodeID, ref sRev);
            return sRev;
        }

        /// <summary>
        /// 获取父节点名称
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="sNodeID">节点ID</param>
        /// <param name="sInfo">父节点路径</param>
        public void GetParentName(GS.DataBase.IDbAccess iDb, string sNodeID, ref string sInfo)
        {
            string strSql = string.Format("select TID,TNAME from LARTYPE where TID in (select" +
                " PTID from LARTYPE where TID={0})", sNodeID);
            DataSet ds = iDb.GetDataSet(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                string sParentID = ds.Tables[0].Rows[0][0].ToString();
                string sNodeName = ds.Tables[0].Rows[0][1].ToString();
                if (sInfo.Length > 0)
                {
                    sInfo = sNodeName + ">" + sInfo;
                }
                else
                {
                    sInfo = sNodeName;
                }

                if (sParentID != "-1")
                {
                    //父节点
                    GetParentName(iDb, sParentID, ref sInfo);
                }
            }

        }
        #endregion

        #region//获取节点编号
        /// <summary>
        /// 获取默认排序编号
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <returns>默认排序编号</returns>
        public int GetDictID(IDbAccess iDb)
        {
            int iRev = 1;

            string strSql = string.Format("select MAX(TID) from LARTYPE");
            object o = iDb.GetFirstColumn(strSql);
            if (o != null && o.ToString().Length > 0 && Convert.ToInt32(o) > 0)
            {
                iRev = Convert.ToInt32(o) + 1;
            }

            return iRev;
        }
        #endregion

        #region//获取节点排序方式
        /// <summary>
        /// 获取默认排序编号
        /// </summary>
        /// <param name="iDb">数据库对象</param>
        /// <param name="iParentID">父节点ID</param>
        /// <returns>默认排序编号</returns>
        public int GetNodeOrder(IDbAccess iDb, int iParentID)
        {
            int iRev = 1;

            string strSql = string.Format("select MAX(TOID) from LARTYPE where PTID='{0}'", iParentID);
            object o = iDb.GetFirstColumn(strSql);
            if (o != null && o.ToString().Length > 0 && Convert.ToInt32(o) > 0)
            {
                iRev = Convert.ToInt32(o) + 1;
            }

            return iRev;
        }
        #endregion

        /// <summary>
        /// 获取所有子节点的CODE，包括自己('a','b')
        /// </summary>
        /// <param name="iDb">数据库连接对象</param>
        /// <param name="sPCode">父节点CODE</param>
        /// <returns></returns>
        public string GetALLCode(GS.DataBase.IDbAccess iDb, string sPCode)
        {
            LARTYPE larType = new LARTYPE();
            larType = larType.Get(iDb, " and TCODE='" + sPCode + "'");

            List<LARTYPE> listRes = new List<LARTYPE>();
            larType.GetChildCode(iDb, larType.TID, ref listRes);
            string sLarType = "'" + sPCode + "',";
            foreach (LARTYPE model in listRes)
            {
                sLarType += "'" + model.TCODE + "',";
            }
            sLarType = sLarType.Trim(',');
            return sLarType;
        }
        #endregion
    }
}